import 'package:get/get.dart';
import 'package:xiaomi_shop/app/api/api.dart';
import 'package:xiaomi_shop/app/services/xhttp.dart';
import 'package:xiaomi_shop/models/index.dart';

class CategoryController extends GetxController {
  // 当前选中的父类ID
  final index = 0.obs;
  var catePlist = <ShopCategoryModel>[].obs;
  var cateClist = <ShopCategoryModel>[].obs;
  @override
  void onInit() {
    super.onInit();
    // 获取一级分类列表
    getCatePlistData();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  // 获取一级分类列表
  void getCatePlistData() {
    XHttp.getInstance().get(Api.pcate).then((value) {
      Result result = value as Result;
      List list = result.data["result"] as List;
      catePlist.value = list
          .map((e) => ShopCategoryModel.fromJson(e as Map<String, dynamic>))
          .toList();

      getCateClistData(catePlist.value.first.id!);
    });
  }

  // 获取二级分类列表
  void getCateClistData(String id) {
    XHttp.getInstance().get(Api.pcate, {"pid": id}).then((value) {
      Result result = value as Result;
      List list = result.data["result"] as List;
      cateClist.value = list
          .map((e) => ShopCategoryModel.fromJson(e as Map<String, dynamic>))
          .toList();
    });
  }
}
